<?php

namespace App\Admin\Controllers;

use App\Customer;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use function foo\func;

class CustomerController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('客户管理')
            ->description('在这里管理您的全部客户')
            ->breadcrumb(
                ['text' => '客户列表']
            )
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->breadcrumb(
                ['text' => '客户列表', 'url' => '/customers'],
                ['text' => '客户详情']
            )
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新用户')
            ->description('填写新用户信息')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Customer);

        $grid->id('Id')->sortable();
        $grid->name('姓名');
        $grid->gender('性别')->sortable()->using(['0' => '男', '1' => '女']);
        $grid->phone('联系电话');
        $grid->email('Email');
        $grid->orders('订单数')->display(function ($orders){
            $count = count($orders);
            return "<span class='label label-primary'>$count</span>";
        });
        $grid->bookings('入住次数')->display(function($bookings){
            $count = count($bookings);
            return "<span class='label label-success'>$count</span>";
        });
        $grid->actions(function($actions){
            $actions->append('
                <a href=""><i class="fa fa-eye"></i></a>
            ');
        });

        //filter
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter){
                $filter->like('name', '姓名');
                $filter->like('phone', '联系电话');
            });
            $filter->column(1/2, function ($filter){
                $filter->like('email', '邮箱');
                $filter->equal('gender', '性别')->radio([
                    '' => '所有',
                    0 => '男',
                    1 => '女'
                ]);
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Customer::findOrFail($id));

        $show->id('客户Id');
        $show->created_at('注册时间');
        $show->updated_at('修改时间');
        $show->name('姓名');
        $show->gender('性别');
        $show->phone('联系电话');
        $show->email('Email');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Customer);
        $form->setTitle('客户信息');

        $form->text('name', '姓名');
        $form->radio('gender', '性别')->options([
            0 => '男',
            1 => '女',
        ]);
        $form->mobile('phone', '联系电话');
        $form->email('email', 'Email');
        $form->password('password', '密码')->rules('required|confirmed')
            ->default(function ($form){
                return $form->model()->password;
            });
        $form->password('password_confirmation', '确认密码')->rules('required')
            ->default(function($form){
                return $form->model()->password;
            });
        $form->ignore(['password_confirmation']);
        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });

        return $form;
    }
}
